package com.jh.fcsm.controller.inf;

import com.github.pagehelper.PageInfo;
import com.jh.fcsm.beans.inf.InfApp;
import com.jh.fcsm.beans.inf.vo.InfAppVo;
import com.jh.fcsm.common.BaseController;
import com.jh.fcsm.common.RestApiResponse;
import com.jh.fcsm.common.annotation.RepeatSubAnnotation;
import com.jh.fcsm.common.annotation.SystemLogAnnotation;
import com.jh.fcsm.service.inf.InfAppService;
import com.jh.fcsm.util.security.RSAEncrypt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

/**
 * 应用令牌秘钥配置Controller
 *
 * @author linqiang
 * @date 2022-03-01 12:54:48
 */
@RestController
@RequestMapping("/inf/app")
@Api(value = "应用令牌秘钥配置", tags = "应用令牌秘钥配置")
public class InfAppController extends BaseController {
    @Autowired
    private InfAppService infAppService;

    private static final String PER_PREFIX = "btn:inf:app:";

    /**
     * @param infApp 应用令牌秘钥配置数据 json
     * @return RestApiResponse<?>
     * @Description: 新增应用令牌秘钥配置
     * @Author: linqiang
     */
    @RepeatSubAnnotation
    @PostMapping("/save")
    @ApiOperation(value = "新增应用令牌秘钥配置", notes = "新增应用令牌秘钥配置")
    @SystemLogAnnotation(type = "令牌秘钥配置", value = "新增应用令牌秘钥配置")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "save')")
    public RestApiResponse<?> save(@RequestBody InfApp infApp) {
        String id = infAppService.saveOrUpdate(infApp);
        return RestApiResponse.ok(id);
    }

    /**
     * @param infApp 应用令牌秘钥配置数据 json
     * @return RestApiResponse<?>
     * @Description: 修改应用令牌秘钥配置
     * @Author: linqiang
     */
    @RepeatSubAnnotation
    @PostMapping("/update")
    @ApiOperation(value = "修改应用令牌秘钥配置", notes = "修改应用令牌秘钥配置")
    @SystemLogAnnotation(type = "令牌秘钥配置", value = "修改应用令牌秘钥配置")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "update')")
    public RestApiResponse<?> update(@RequestBody InfApp infApp) {
        String id = infAppService.saveOrUpdate(infApp);
        return RestApiResponse.ok(id);
    }

    /**
     * @param appMethodList 应用令牌秘钥配置数据 json
     * @return RestApiResponse<?>
     * @Description: 修改应用令牌秘钥配置
     * @Author: linqiang
     */
    @RepeatSubAnnotation
    @PostMapping("/selectMethod")
    @ApiOperation(value = "应用key配置权限", notes = "应用key配置权限")
    @SystemLogAnnotation(type = "令牌秘钥配置", value = "应用key配置权限")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "method')")
    public RestApiResponse<?> selectMethod(@RequestBody List<Map<String, String>> appMethodList) {
        infAppService.insertAppKeyMethodList(appMethodList);
        return RestApiResponse.ok();
    }

    /**
     * @param ids
     * @return RestApiResponse<?>
     * @Description: 批量删除应用令牌秘钥配置(判断 关联数据是否可以删除)
     * @Author: linqiang
     */
    @RepeatSubAnnotation
    @PostMapping("/delete")
    @ApiOperation(value = "批量删除应用令牌秘钥配置", notes = "批量删除应用令牌秘钥配置")
    @SystemLogAnnotation(type = "令牌秘钥配置", value = "批量删除应用令牌秘钥配置")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "delete')")
    public RestApiResponse<?> delete(@RequestBody List<String> ids) {
        //判断 关联数据是否可以删除
        infAppService.delete(ids);
        return RestApiResponse.ok();
    }

    /**
     * @param appKey
     * @return RestApiResponse<?>
     * @Description: 根据appkey查询所有方法
     * @Author: linqiang
     */
    @GetMapping("/findMethodByAppKey")
    @ApiOperation(value = "根据appkey查询所有方法", notes = "根据appkey查询所有方法")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "findmethod')")
    public RestApiResponse<?> findMethodByAppKey(@RequestParam("appKey") String appKey) {
        List<String> methods = infAppService.findMethodByAppKey(appKey);
        return RestApiResponse.ok(methods);
    }

    /**
     * @param id
     * @return RestApiResponse<?>
     * @Description: 查询应用令牌秘钥配置详情
     * @Author: linqiang
     */
    @GetMapping("/findById")
    @ApiOperation(value = "查询应用令牌秘钥配置详情", notes = "查询应用令牌秘钥配置详情")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "find')")
    public RestApiResponse<?> findById(@RequestParam("id") String id) {
        InfApp infApp = infAppService.findById(id);
        return RestApiResponse.ok(infApp);
    }

    /**
     * @param infAppVo 应用令牌秘钥配置 查询条件
     * @return RestApiResponse<?>
     * @Description: 分页查询应用令牌秘钥配置
     * @Author: linqiang
     */
    @PostMapping("/findPageByQuery")
    @ApiOperation(value = "分页查询应用令牌秘钥配置", notes = "分页查询应用令牌秘钥配置")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "query')")
    public RestApiResponse<?> findPageByQuery(@RequestBody InfAppVo infAppVo) {
        PageInfo<InfApp> infApp = infAppService.findPageByQuery(infAppVo);
        return RestApiResponse.ok(infApp);
    }

    /**
     *@Description: 生成公钥私钥
     *@return RestApiResponse<?>
     *@Author: qiubinbin
     */
    @PostMapping("/rsaEncrypt")
    @ApiOperation(value = "生成公钥私钥", notes = "生成公钥私钥")
    public RestApiResponse<?> rsaEncrypt() throws NoSuchAlgorithmException {
        Map<Integer, String> map =  RSAEncrypt.getSecret();
        return RestApiResponse.ok(map);
    }

}
